#include "equipment.h"
#include "ui_addequip.h"

const char* EquipmentFilePath = "./equipment.json";

/*----------------------------------Addequip Class-----------------------------------*/
AddEquip::AddEquip(QWidget *parent)
    : QDialog(parent), ui(new Ui::AddEquip)
{
    ui->setupUi(this);
    setWindowTitle(tr("装备添加"));
    connect(ui->comboBox_eqType,&QComboBox::currentIndexChanged,this,&AddEquip::changeStackWidgetIndex);
    connect(ui->checkBox_knight,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_shooter,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_fighter,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_clergy,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_enchanter,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_clothArmor,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_leatherArmor,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_lightArmor,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_heavyArmor,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->checkBox_plateArmor,&QCheckBox::clicked,this,&AddEquip::checkBoxOnlyOne);
    connect(ui->pushButton_pic,&QPushButton::clicked,this,&AddEquip::readPictureValue);
    // 接收请求
    connect(&networkManager,&QNetworkAccessManager::finished,this,&AddEquip::getReply);
    // 发送获取ocr识别所需Token的请求
    requestAccessToken();
}

AddEquip::~AddEquip()
{
    qDebug() << "add equip destruct";
}

// QDialog当前页面数据复位成null
void AddEquip::resetCurrentIndexData()
{
    int index = ui->stackedWidget->currentIndex();
    // 清空相关数据
    if(ui->comboBox_eqType->currentText() == tr("武器"))
    {
        ui->lineEdit_weponName->setText("");
        ui->checkBox_knight->setCheckState(Qt::Unchecked);
        ui->checkBox_shooter->setCheckState(Qt::Unchecked);
        ui->checkBox_fighter->setCheckState(Qt::Unchecked);
        ui->checkBox_clergy->setCheckState(Qt::Unchecked);
        ui->checkBox_enchanter->setCheckState(Qt::Unchecked);
    }
    else if(ui->comboBox_eqType->currentText() == tr("防具"))
    {
        ui->lineEdit_armorName->setText("");
        ui->checkBox_clothArmor->setCheckState(Qt::Unchecked);
        ui->checkBox_leatherArmor->setCheckState(Qt::Unchecked);
        ui->checkBox_lightArmor->setCheckState(Qt::Unchecked);
        ui->checkBox_heavyArmor->setCheckState(Qt::Unchecked);
        ui->checkBox_plateArmor->setCheckState(Qt::Unchecked);
    }
    else if(ui->comboBox_eqType->currentText() == tr("首饰"))
    {
        ui->lineEdit_jewName->setText("");
    }
    else if(ui->comboBox_eqType->currentText() == tr("辅助装备"))
    {
        ui->lineEdit_auxName->setText("");
    }
    ad_strength = 0;
    ad_intelligence = 0;
    ad_physicalAttack = 0;
    ad_magicalAttack = 0;
    ad_physicalCriticalProb = 0;
    ad_magicalCriticalProb = 0;
    ad_physicalCriticalValue = 0;
    ad_magicalCriticalValue = 0;
    ad_strengthBonus = 0;
    ad_intelligenceBonus = 0;
    ad_physicalAttackBonus = 0;
    ad_magicalAttackBonus = 0;
    ad_skillBonus = 0;
    ad_damageBonus = 0;
    ad_criticalDamageBonus = 0;
    ad_extraDamageBonus = 0;
    ad_elementExtraDamageBonus = 0;
    ad_elementBonus = 0;
    entryValueIntoSpinbox(index);
}

// QDialog确认按钮按下的功能
void AddEquip::accept()
{
    if(ui->comboBox_eqType->currentText() == tr("武器"))
    {
        if(ui->lineEdit_weponName->text() == "")
        {
            QMessageBox::information(this,"提示","请填写武器名称");
            return;
        }
        else if(!ui->checkBox_knight->isChecked() && !ui->checkBox_shooter->isChecked()
                 && !ui->checkBox_fighter->isChecked() && !ui->checkBox_clergy->isChecked()
                 && !ui->checkBox_enchanter->isChecked())
        {
            QMessageBox::information(this,"提示","请选择使用该武器的角色");
            return;
        }
    }
    else if(ui->comboBox_eqType->currentText()==tr("防具"))
    {
        if(ui->lineEdit_armorName->text() == "")
        {
            QMessageBox::information(this,"提示","请填写防具套装名称");
            return;
        }
        else if(!ui->checkBox_clothArmor->isChecked()
                 && !ui->checkBox_leatherArmor->isChecked()
                 && !ui->checkBox_lightArmor->isChecked()
                 && !ui->checkBox_heavyArmor->isChecked()
                 && !ui->checkBox_plateArmor->isChecked())
        {
            QMessageBox::information(this,"提示","请选择防具类型");
            return;
        }
    }
    else if(ui->comboBox_eqType->currentText() == tr("首饰")
             && ui->lineEdit_jewName->text() == "")
    {
        QMessageBox::information(this,"提示","请填写首饰套装名称");
        return;
    }
    else if(ui->comboBox_eqType->currentText() == tr("辅助装备")
             && ui->lineEdit_auxName->text() == "")
    {
        QMessageBox::information(this,"提示","请填写辅助装备名称");
        return;
    }
    // 把装备信息写入json文件
    writeDataIntoJson();
    // 把当前页面的数据全部复位
    resetCurrentIndexData();
    // 关闭页面
    setResult(QDialog::Accepted);
    emit this->accepted();
    this->close();
}

// QDialog确认按钮按下的功能
void AddEquip::reject()
{
    // 把当前页面的数据全部复位
    resetCurrentIndexData();
    // 关闭页面
    setResult(QDialog::Rejected);
    emit this->rejected();
    this->hide();
}

void AddEquip::changeStackWidgetIndex(int index)
{
    ui->stackedWidget->setCurrentIndex(index);
    // 清空相关数值
    ad_strength = 0;
    ad_intelligence = 0;
    ad_physicalAttack = 0;
    ad_magicalAttack = 0;
    ad_physicalCriticalProb = 0;
    ad_magicalCriticalProb = 0;
    ad_physicalCriticalValue = 0;
    ad_magicalCriticalValue = 0;
    ad_strengthBonus = 0;
    ad_intelligenceBonus = 0;
    ad_physicalAttackBonus = 0;
    ad_magicalAttackBonus = 0;
    ad_skillBonus = 0;
    ad_damageBonus = 0;
    ad_criticalDamageBonus = 0;
    ad_extraDamageBonus = 0;
    ad_elementExtraDamageBonus = 0;
    ad_elementBonus = 0;
}

void AddEquip::writeDataIntoJson()
{
    QFile jsonFile(EquipmentFilePath);
    if(!jsonFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QMessageBox::information(this,"提示","JSON文件打开错误");
        return;
    }
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonFile.readAll());
    jsonFile.close();
    if (!jsonDoc.isObject())
    {
        QMessageBox::information(this,"提示","JSON文件格式错误，无法写入");
        return;
    }
    // 把新的装备信息添加到json文件中
    QJsonObject rootObject = jsonDoc.object();
    if(ui->comboBox_eqType->currentText() == tr("武器"))
    {
        writeWeponInfo(rootObject);
    }
    else if(ui->comboBox_eqType->currentText() == tr("防具"))
    {
        writeArmorInfo(rootObject);
    }
    else if(ui->comboBox_eqType->currentText() == tr("首饰"))
    {
        writeJewelryInfo(rootObject);
    }
    else if(ui->comboBox_eqType->currentText() == tr("辅助装备"))
    {
        writeAuxiliaryInfo(rootObject);
    }
    // 写入文件
    jsonDoc = QJsonDocument(rootObject);
    if(!jsonFile.open(QIODevice::WriteOnly | QIODevice::Text|
                       QIODevice::Truncate))
    {
        QMessageBox::information(this,"提示","JSON文件写入错误");
        return;
    }
    jsonFile.write(jsonDoc.toJson());
    jsonFile.close();
}

void AddEquip::writeWeponInfo(QJsonObject &rootObject)
{
    QJsonObject weponObject = rootObject["武器"].toObject();
    if(ui->checkBox_knight->isChecked()) // 写入鬼剑士武器
    {
        // 找到鬼剑士对象，并获取其数组
        QJsonArray knightWepon = weponObject.value("鬼剑士").toArray();
        // 创建新装备对象，并添加到鬼剑士对象数组中
        QJsonObject newWepon {
                             {"武器名称",ui->lineEdit_weponName->text()},
                             {JSON_STR,ui->spinBox_weponSTR->value()},
                             {JSON_INT,ui->spinBox_weponINT->value()},
                             {JSON_STRB,ui->spinBox_weponSTRB->value()},
                             {JSON_INTB,ui->spinBox_weponINTB->value()},
                             {JSON_PATK,ui->spinBox_weponPATK->value()},
                             {JSON_MATK,ui->spinBox_weponMATK->value()},
                             {JSON_PATKB,ui->spinBox_weponPATKB->value()},
                             {JSON_MATKB,ui->spinBox_weponMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_weponPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_weponMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_weponPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_weponMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_weponSKIB->value()},
                             {JSON_DAMB,ui->spinBox_weponDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_weponCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_weponEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_weponELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_weponELEB->value()},
                             };
        knightWepon.append(newWepon);
        weponObject["鬼剑士"] = knightWepon;
        rootObject["武器"] = weponObject;
    }
    else if(ui->checkBox_shooter->isChecked()) // 写入神枪手武器
    {
        // 找到神枪手对象，并获取其数组
        QJsonArray shooterWepon = weponObject.value("神枪手").toArray();
        // 创建新装备对象，并添加到神枪手对象数组中
        QJsonObject newWepon {
                             {"武器名称",ui->lineEdit_weponName->text()},
                             {JSON_STR,ui->spinBox_weponSTR->value()},
                             {JSON_INT,ui->spinBox_weponINT->value()},
                             {JSON_STRB,ui->spinBox_weponSTRB->value()},
                             {JSON_INTB,ui->spinBox_weponINTB->value()},
                             {JSON_PATK,ui->spinBox_weponPATK->value()},
                             {JSON_MATK,ui->spinBox_weponMATK->value()},
                             {JSON_PATKB,ui->spinBox_weponPATKB->value()},
                             {JSON_MATKB,ui->spinBox_weponMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_weponPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_weponMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_weponPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_weponMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_weponSKIB->value()},
                             {JSON_DAMB,ui->spinBox_weponDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_weponCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_weponEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_weponELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_weponELEB->value()},
                             };
        shooterWepon.append(newWepon);
        weponObject["神枪手"] = shooterWepon;
        rootObject["武器"] = weponObject;

    }
    else if(ui->checkBox_fighter->isChecked()) // 写入格斗家武器
    {
        // 找到格斗家对象，并获取其数组
        QJsonArray fighterWepon = weponObject.value("格斗家").toArray();
        // 创建新装备对象，并添加到格斗家对象数组中
        QJsonObject newWepon {
                             {"武器名称",ui->lineEdit_weponName->text()},
                             {JSON_STR,ui->spinBox_weponSTR->value()},
                             {JSON_INT,ui->spinBox_weponINT->value()},
                             {JSON_STRB,ui->spinBox_weponSTRB->value()},
                             {JSON_INTB,ui->spinBox_weponINTB->value()},
                             {JSON_PATK,ui->spinBox_weponPATK->value()},
                             {JSON_MATK,ui->spinBox_weponMATK->value()},
                             {JSON_PATKB,ui->spinBox_weponPATKB->value()},
                             {JSON_MATKB,ui->spinBox_weponMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_weponPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_weponMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_weponPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_weponMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_weponSKIB->value()},
                             {JSON_DAMB,ui->spinBox_weponDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_weponCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_weponEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_weponELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_weponELEB->value()},
                             };
        fighterWepon.append(newWepon);
        weponObject["格斗家"] = fighterWepon;
        rootObject["武器"] = weponObject;
    }
    else if(ui->checkBox_clergy->isChecked()) // 写入圣职者武器
    {
        // 找到圣职者对象，并获取其数组
        QJsonArray clergyWepon = weponObject.value("圣职者").toArray();
        // 创建新装备对象，并添加到圣职者对象数组中
        QJsonObject newWepon {
                             {"武器名称",ui->lineEdit_weponName->text()},
                             {JSON_STR,ui->spinBox_weponSTR->value()},
                             {JSON_INT,ui->spinBox_weponINT->value()},
                             {JSON_STRB,ui->spinBox_weponSTRB->value()},
                             {JSON_INTB,ui->spinBox_weponINTB->value()},
                             {JSON_PATK,ui->spinBox_weponPATK->value()},
                             {JSON_MATK,ui->spinBox_weponMATK->value()},
                             {JSON_PATKB,ui->spinBox_weponPATKB->value()},
                             {JSON_MATKB,ui->spinBox_weponMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_weponPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_weponMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_weponPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_weponMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_weponSKIB->value()},
                             {JSON_DAMB,ui->spinBox_weponDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_weponCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_weponEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_weponELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_weponELEB->value()},
                             };
        clergyWepon.append(newWepon);
        weponObject["圣职者"] = clergyWepon;
        rootObject["武器"] = weponObject;
    }
    else if(ui->checkBox_enchanter->isChecked()) // 写入魔法师武器
    {
        // 找到魔法师对象，并获取其数组
        QJsonArray enchanterWepon = weponObject.value("魔法师").toArray();
        // 创建新装备对象，并添加到魔法师对象数组中
        QJsonObject newWepon {
                             {"武器名称",ui->lineEdit_weponName->text()},
                             {JSON_STR,ui->spinBox_weponSTR->value()},
                             {JSON_INT,ui->spinBox_weponINT->value()},
                             {JSON_STRB,ui->spinBox_weponSTRB->value()},
                             {JSON_INTB,ui->spinBox_weponINTB->value()},
                             {JSON_PATK,ui->spinBox_weponPATK->value()},
                             {JSON_MATK,ui->spinBox_weponMATK->value()},
                             {JSON_PATKB,ui->spinBox_weponPATKB->value()},
                             {JSON_MATKB,ui->spinBox_weponMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_weponPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_weponMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_weponPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_weponMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_weponSKIB->value()},
                             {JSON_DAMB,ui->spinBox_weponDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_weponCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_weponEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_weponELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_weponELEB->value()},
                             };
        enchanterWepon.append(newWepon);
        weponObject["魔法师"] = enchanterWepon;
        rootObject["武器"] = weponObject;
    }
}

void AddEquip::writeArmorInfo(QJsonObject &rootObject)
{
    QJsonObject armorObject = rootObject["防具"].toObject();
    if(ui->checkBox_clothArmor->isChecked()) // 写入布甲
    {
        QJsonArray clothArmor = armorObject.value("布甲").toArray();
        QJsonObject newArmor {
                             {"防具套装名称",ui->lineEdit_armorName->text()},
                             {JSON_STR,ui->spinBox_armorSTR->value()},
                             {JSON_INT,ui->spinBox_armorINT->value()},
                             {JSON_STRB,ui->spinBox_armorSTRB->value()},
                             {JSON_INTB,ui->spinBox_armorINTB->value()},
                             {JSON_PATK,ui->spinBox_armorPATK->value()},
                             {JSON_MATK,ui->spinBox_armorMATK->value()},
                             {JSON_PATKB,ui->spinBox_armorPATKB->value()},
                             {JSON_MATKB,ui->spinBox_armorMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_armorPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_armorMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_armorPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_armorMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_armorSKIB->value()},
                             {JSON_DAMB,ui->spinBox_armorDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_armorCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_armorEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_armorELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_armorELEB->value()},
                             };
        clothArmor.append(newArmor);
        armorObject["布甲"] = clothArmor;
        rootObject["防具"] = armorObject;
    }
    else if(ui->checkBox_leatherArmor->isChecked()) // 写入皮甲
    {
        QJsonArray leatherArmor = armorObject.value("皮甲").toArray();
        QJsonObject newArmor {
                             {"防具套装名称",ui->lineEdit_armorName->text()},
                             {JSON_STR,ui->spinBox_armorSTR->value()},
                             {JSON_INT,ui->spinBox_armorINT->value()},
                             {JSON_STRB,ui->spinBox_armorSTRB->value()},
                             {JSON_INTB,ui->spinBox_armorINTB->value()},
                             {JSON_PATK,ui->spinBox_armorPATK->value()},
                             {JSON_MATK,ui->spinBox_armorMATK->value()},
                             {JSON_PATKB,ui->spinBox_armorPATKB->value()},
                             {JSON_MATKB,ui->spinBox_armorMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_armorPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_armorMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_armorPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_armorMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_armorSKIB->value()},
                             {JSON_DAMB,ui->spinBox_armorDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_armorCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_armorEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_armorELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_armorELEB->value()},
                             };
        leatherArmor.append(newArmor);
        armorObject["皮甲"] = leatherArmor;
        rootObject["防具"] = armorObject;
    }
    else if(ui->checkBox_lightArmor->isChecked()) // 写入轻甲
    {
        QJsonArray lightArmor = armorObject.value("轻甲").toArray();
        QJsonObject newArmor {
                             {"防具套装名称",ui->lineEdit_armorName->text()},
                             {JSON_STR,ui->spinBox_armorSTR->value()},
                             {JSON_INT,ui->spinBox_armorINT->value()},
                             {JSON_STRB,ui->spinBox_armorSTRB->value()},
                             {JSON_INTB,ui->spinBox_armorINTB->value()},
                             {JSON_PATK,ui->spinBox_armorPATK->value()},
                             {JSON_MATK,ui->spinBox_armorMATK->value()},
                             {JSON_PATKB,ui->spinBox_armorPATKB->value()},
                             {JSON_MATKB,ui->spinBox_armorMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_armorPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_armorMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_armorPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_armorMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_armorSKIB->value()},
                             {JSON_DAMB,ui->spinBox_armorDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_armorCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_armorEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_armorELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_armorELEB->value()},
                             };
        lightArmor.append(newArmor);
        armorObject["轻甲"] = lightArmor;
        rootObject["防具"] = armorObject;
    }
    else if(ui->checkBox_heavyArmor->isChecked()) // 写入重甲
    {
        QJsonArray heavyArmor = armorObject.value("重甲").toArray();
        QJsonObject newArmor {
                             {"防具套装名称",ui->lineEdit_armorName->text()},
                             {JSON_STR,ui->spinBox_armorSTR->value()},
                             {JSON_INT,ui->spinBox_armorINT->value()},
                             {JSON_STRB,ui->spinBox_armorSTRB->value()},
                             {JSON_INTB,ui->spinBox_armorINTB->value()},
                             {JSON_PATK,ui->spinBox_armorPATK->value()},
                             {JSON_MATK,ui->spinBox_armorMATK->value()},
                             {JSON_PATKB,ui->spinBox_armorPATKB->value()},
                             {JSON_MATKB,ui->spinBox_armorMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_armorPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_armorMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_armorPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_armorMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_armorSKIB->value()},
                             {JSON_DAMB,ui->spinBox_armorDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_armorCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_armorEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_armorELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_armorELEB->value()},
                             };
        heavyArmor.append(newArmor);
        armorObject["重甲"] = heavyArmor;
        rootObject["防具"] = armorObject;
    }
    else if(ui->checkBox_plateArmor->isChecked()) // 写入板甲
    {
        QJsonArray plateArmor = armorObject.value("板甲").toArray();
        QJsonObject newArmor {
                             {"防具套装名称",ui->lineEdit_armorName->text()},
                             {JSON_STR,ui->spinBox_armorSTR->value()},
                             {JSON_INT,ui->spinBox_armorINT->value()},
                             {JSON_STRB,ui->spinBox_armorSTRB->value()},
                             {JSON_INTB,ui->spinBox_armorINTB->value()},
                             {JSON_PATK,ui->spinBox_armorPATK->value()},
                             {JSON_MATK,ui->spinBox_armorMATK->value()},
                             {JSON_PATKB,ui->spinBox_armorPATKB->value()},
                             {JSON_MATKB,ui->spinBox_armorMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_armorPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_armorMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_armorPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_armorMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_armorSKIB->value()},
                             {JSON_DAMB,ui->spinBox_armorDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_armorCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_armorEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_armorELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_armorELEB->value()},
                             };
        plateArmor.append(newArmor);
        armorObject["板甲"] = plateArmor;
        rootObject["防具"] = armorObject;
    }
}

void AddEquip::writeJewelryInfo(QJsonObject &rootObject)
{
    QJsonArray jewelry = rootObject.value("首饰").toArray();
    QJsonObject newJewelry {
                           {"首饰套装名称",ui->lineEdit_jewName->text()},
                           {JSON_STR,ui->spinBox_jewSTR->value()},
                           {JSON_INT,ui->spinBox_jewINT->value()},
                           {JSON_STRB,ui->spinBox_jewSTRB->value()},
                           {JSON_INTB,ui->spinBox_jewINTB->value()},
                           {JSON_PATK,ui->spinBox_jewPATK->value()},
                           {JSON_MATK,ui->spinBox_jewMATK->value()},
                           {JSON_PATKB,ui->spinBox_jewPATKB->value()},
                           {JSON_MATKB,ui->spinBox_jewMATKB->value()},
                           {JSON_PCRIP,ui->spinBox_jewPCRIP->value()},
                           {JSON_MCRIP,ui->spinBox_jewMCRIP->value()},
                           {JSON_PCRIV,ui->spinBox_jewPCRIV->value()},
                           {JSON_MCRIV,ui->spinBox_jewMCRIV->value()},
                           {JSON_SKIB,ui->spinBox_jewSKIB->value()},
                           {JSON_DAMB,ui->spinBox_jewDAMB->value()},
                           {JSON_CRIDAMB,ui->spinBox_jewCRIDAMB->value()},
                           {JSON_EXTB,ui->spinBox_jewEXTDAMB->value()},
                           {JSON_ELEEXTB,ui->spinBox_jewELEEXTB->value()},
                           {JSON_ELEB,ui->spinBox_jewELEB->value()},
                           };
    jewelry.append(newJewelry);
    rootObject["首饰"] = jewelry;
}

void AddEquip::writeAuxiliaryInfo(QJsonObject &rootObject)
{
    QJsonArray auxiliary = rootObject.value("辅助装备").toArray();
    QJsonObject newAuxiliary {
                             {"辅助装备名称",ui->lineEdit_auxName->text()},
                             {JSON_STR,ui->spinBox_auxSTR->value()},
                             {JSON_INT,ui->spinBox_auxINT->value()},
                             {JSON_STRB,ui->spinBox_auxSTRB->value()},
                             {JSON_INTB,ui->spinBox_auxINTB->value()},
                             {JSON_PATK,ui->spinBox_auxPATK->value()},
                             {JSON_MATK,ui->spinBox_auxMATK->value()},
                             {JSON_PATKB,ui->spinBox_auxPATKB->value()},
                             {JSON_MATKB,ui->spinBox_auxMATKB->value()},
                             {JSON_PCRIP,ui->spinBox_auxPCRIP->value()},
                             {JSON_MCRIP,ui->spinBox_auxMCRIP->value()},
                             {JSON_PCRIV,ui->spinBox_auxPCRIV->value()},
                             {JSON_MCRIV,ui->spinBox_auxMCRIV->value()},
                             {JSON_SKIB,ui->spinBox_auxSKIB->value()},
                             {JSON_DAMB,ui->spinBox_auxDAMB->value()},
                             {JSON_CRIDAMB,ui->spinBox_auxCRIDAMB->value()},
                             {JSON_EXTB,ui->spinBox_auxEXTDAMB->value()},
                             {JSON_ELEEXTB,ui->spinBox_auxELEEXTB->value()},
                             {JSON_ELEB,ui->spinBox_auxELEB->value()},
                             };
    auxiliary.append(newAuxiliary);
    rootObject["辅助装备"] = auxiliary;
}

void AddEquip::readPictureValue()
{
    static QString filePath = "./";
    QString fileName = QFileDialog::getOpenFileName(this,tr("图片选择"),
                                                    filePath, tr("Image Files (*.png *.jpg *.bmp)"));
    if(fileName == nullptr)
        return;
    filePath = fileName.sliced(0,fileName.lastIndexOf("/"));
    QBuffer buffer;
    buffer.open(QIODevice::WriteOnly);
    QImage pixel(fileName);
    pixel.save(&buffer,"png");
    base64img = buffer.data().toBase64().toPercentEncoding();
    // 发送获取ocr识别结果的请求
    requestOcrResults();
}

void AddEquip::requestAccessToken()
{
    // 没有config.ini文件则创建一个
    QString fileName = "./config.ini";
    if(!QFile::exists(fileName))
    {
        QSettings config(fileName,QSettings::IniFormat);
        config.setValue("client_id","");
        config.setValue("client_secret","");
    }
    // 获取验证信息
    QSettings config(fileName,QSettings::IniFormat);
    QString client_id = config.value("client_id").toString();
    QString client_secret = config.value("client_secret").toString();
    // 配置请求url信息
    QUrl token_url("https://aip.baidubce.com/oauth/2.0/token");
    QUrlQuery search_parameter;
    search_parameter.addQueryItem("client_id",client_id);
    search_parameter.addQueryItem("client_secret",client_secret);
    search_parameter.addQueryItem("grant_type","client_credentials");
    token_url.setQuery(search_parameter); // 添加查询参数
    // 建立并发送请求
    QNetworkRequest request2;
    request2.setUrl(token_url);
    request2.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    networkManager.post(request2,QByteArray());
}

void AddEquip::requestOcrResults()
{
    if(accessToken.isEmpty())
        return;
    QUrl ocr_url("https://aip.baidubce.com/rest/2.0/ocr/v1/webimage");
    QUrlQuery search_parameter;
    search_parameter.addQueryItem("access_token",accessToken);
    ocr_url.setQuery(search_parameter); // 添加查询参数
    // 建立并发送请求
    QNetworkRequest request;
    request.setUrl(ocr_url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    QUrlQuery req_body; // 请求体
    req_body.addQueryItem("image",base64img);
    req_body.addQueryItem("detect_direction","false");
    req_body.addQueryItem("detect_language","false");
    QByteArray body_data = req_body.query().toUtf8();
    networkManager.post(request,body_data);
}

void AddEquip::getReply(QNetworkReply *reply)
{
    QString text = reply->readAll();
    // qDebug() << text;
    QJsonDocument json = QJsonDocument::fromJson(text.toUtf8());
    QJsonObject obj = json.object();
    if(!obj.value("error_msg").toString().isEmpty())
    {
        QMessageBox::information(this,"提示",tr("ocr识别出错\n错误信息:%1").
                                               arg(obj.value("error_msg").toString()));
        return;
    }
    if(!obj.value("error_description").toString().isEmpty())
    {
        QMessageBox::information(this,"提示",tr("未填写config.ini文件\n或填写的信息无效"));
        return;
    }
    if(obj.contains("access_token"))
    {
        QJsonValue jsonValue = obj.value("access_token");
        accessToken = jsonValue.toString();
    }
    else if(obj.contains("words_result"))
    {
        QJsonValue jsonValue = obj.value("words_result");
        for(int var = 0; var < jsonValue.toArray().size(); var++)
        {
            QString str = jsonValue[var].toObject().value("words").toString();
            if(str.contains(_STR))
                processPictureValue(_STR,str);
            else if(str.contains(_INT))
                processPictureValue(_INT,str);
            else if(str.contains(_PATK))
                processPictureValue(_PATK,str);
            else if(str.contains(_MATK))
                processPictureValue(_MATK,str);
            else if(str.contains(_PCRIP))
                processPictureValue(_PCRIP,str);
            else if(str.contains(_MCRIP))
                processPictureValue(_MCRIP,str);
            else if(str.contains(_PCRIV))
                processPictureValue(_PCRIV,str);
            else if(str.contains(_MCRIV))
                processPictureValue(_MCRIV,str);
            else if(str.contains(_SKI))
                processPictureValue(_SKI,str);
            else if(str.contains(_CRIDAM)) // 暴击伤害文字的识别要在伤害的前面,否则暴击伤害也会被识别成伤害
                processPictureValue(_CRIDAM,str);
            else if(str.contains(_DAM))
                processPictureValue(_DAM,str);
            else if(str.contains(_ELE))
                processPictureValue(_ELE,str);
            else if(str.contains(_EXT))
                processPictureValue(_EXT,str);
            else if(str.contains(_ELEEXT))
                processPictureValue(_ELEEXT,str);
        }
    }
}

void AddEquip::entryValueIntoSpinbox(int num)
{
    switch (num) {
    case 0:
        ui->spinBox_weponSTR->setValue(ad_strength);
        ui->spinBox_weponINT->setValue(ad_intelligence);
        ui->spinBox_weponSTRB->setValue(ad_strengthBonus);
        ui->spinBox_weponINTB->setValue(ad_intelligenceBonus);
        ui->spinBox_weponPATK->setValue(ad_physicalAttack);
        ui->spinBox_weponMATK->setValue(ad_magicalAttack);
        ui->spinBox_weponPATKB->setValue(ad_physicalAttackBonus);
        ui->spinBox_weponMATKB->setValue(ad_magicalAttackBonus);
        ui->spinBox_weponPCRIP->setValue(ad_physicalCriticalProb);
        ui->spinBox_weponMCRIP->setValue(ad_magicalCriticalProb);
        ui->spinBox_weponPCRIV->setValue(ad_physicalCriticalValue);
        ui->spinBox_weponMCRIV->setValue(ad_magicalCriticalValue);
        ui->spinBox_weponDAMB->setValue(ad_damageBonus);
        ui->spinBox_weponCRIDAMB->setValue(ad_criticalDamageBonus);
        ui->spinBox_weponSKIB->setValue(ad_skillBonus);
        ui->spinBox_weponEXTDAMB->setValue(ad_extraDamageBonus);
        ui->spinBox_weponELEEXTB->setValue(ad_elementExtraDamageBonus);
        ui->spinBox_weponELEB->setValue(ad_elementBonus);
        break;
    case 1:
        ui->spinBox_armorSTR->setValue(ad_strength);
        ui->spinBox_armorINT->setValue(ad_intelligence);
        ui->spinBox_armorSTRB->setValue(ad_strengthBonus);
        ui->spinBox_armorINTB->setValue(ad_intelligenceBonus);
        ui->spinBox_armorPATK->setValue(ad_physicalAttack);
        ui->spinBox_armorMATK->setValue(ad_magicalAttack);
        ui->spinBox_armorPATKB->setValue(ad_physicalAttackBonus);
        ui->spinBox_armorMATKB->setValue(ad_magicalAttackBonus);
        ui->spinBox_armorPCRIP->setValue(ad_physicalCriticalProb);
        ui->spinBox_armorMCRIP->setValue(ad_magicalCriticalProb);
        ui->spinBox_armorPCRIV->setValue(ad_physicalCriticalValue);
        ui->spinBox_armorMCRIV->setValue(ad_magicalCriticalValue);
        ui->spinBox_armorDAMB->setValue(ad_damageBonus);
        ui->spinBox_armorCRIDAMB->setValue(ad_criticalDamageBonus);
        ui->spinBox_armorSKIB->setValue(ad_skillBonus);
        ui->spinBox_armorEXTDAMB->setValue(ad_extraDamageBonus);
        ui->spinBox_armorELEEXTB->setValue(ad_elementExtraDamageBonus);
        ui->spinBox_armorELEB->setValue(ad_elementBonus);
        break;
    case 2:
        ui->spinBox_jewSTR->setValue(ad_strength);
        ui->spinBox_jewINT->setValue(ad_intelligence);
        ui->spinBox_jewSTRB->setValue(ad_strengthBonus);
        ui->spinBox_jewINTB->setValue(ad_intelligenceBonus);
        ui->spinBox_jewPATK->setValue(ad_physicalAttack);
        ui->spinBox_jewMATK->setValue(ad_magicalAttack);
        ui->spinBox_jewPATKB->setValue(ad_physicalAttackBonus);
        ui->spinBox_jewMATKB->setValue(ad_magicalAttackBonus);
        ui->spinBox_jewPCRIP->setValue(ad_physicalCriticalProb);
        ui->spinBox_jewMCRIP->setValue(ad_magicalCriticalProb);
        ui->spinBox_jewPCRIV->setValue(ad_physicalCriticalValue);
        ui->spinBox_jewMCRIV->setValue(ad_magicalCriticalValue);
        ui->spinBox_jewDAMB->setValue(ad_damageBonus);
        ui->spinBox_jewCRIDAMB->setValue(ad_criticalDamageBonus);
        ui->spinBox_jewSKIB->setValue(ad_skillBonus);
        ui->spinBox_jewEXTDAMB->setValue(ad_extraDamageBonus);
        ui->spinBox_jewELEEXTB->setValue(ad_elementExtraDamageBonus);
        ui->spinBox_jewELEB->setValue(ad_elementBonus);
        break;
    case 3:
        ui->spinBox_auxSTR->setValue(ad_strength);
        ui->spinBox_auxINT->setValue(ad_intelligence);
        ui->spinBox_auxSTRB->setValue(ad_strengthBonus);
        ui->spinBox_auxINTB->setValue(ad_intelligenceBonus);
        ui->spinBox_auxPATK->setValue(ad_physicalAttack);
        ui->spinBox_auxMATK->setValue(ad_magicalAttack);
        ui->spinBox_auxPATKB->setValue(ad_physicalAttackBonus);
        ui->spinBox_auxMATKB->setValue(ad_magicalAttackBonus);
        ui->spinBox_auxPCRIP->setValue(ad_physicalCriticalProb);
        ui->spinBox_auxMCRIP->setValue(ad_magicalCriticalProb);
        ui->spinBox_auxPCRIV->setValue(ad_physicalCriticalValue);
        ui->spinBox_auxMCRIV->setValue(ad_magicalCriticalValue);
        ui->spinBox_auxDAMB->setValue(ad_damageBonus);
        ui->spinBox_auxCRIDAMB->setValue(ad_criticalDamageBonus);
        ui->spinBox_auxSKIB->setValue(ad_skillBonus);
        ui->spinBox_auxEXTDAMB->setValue(ad_extraDamageBonus);
        ui->spinBox_auxELEEXTB->setValue(ad_elementExtraDamageBonus);
        ui->spinBox_auxELEB->setValue(ad_elementBonus);
        break;
    }
}

void AddEquip::processPictureValue(const QString& strContain, QString str)
{
    if(strContain == _STR)
    {
        if(str.contains("%") && str.contains("+")) // 力量百分比加成
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_strengthBonus += strList[0].toInt();
        }
        else if(str.contains("+")) // 力量加成
        {
            QStringList strList = str.split("+");
            ad_strength += strList[1].toInt();
        }
    }
    else if(strContain == _INT)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_intelligenceBonus += strList[0].toInt();
        }
        else if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_intelligence += strList[1].toInt();
        }
    }
    else if(strContain == _PATK)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_physicalAttackBonus += strList[0].toInt();
        }
        else if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_physicalAttack += strList[1].toInt();
        }
    }
    else if(strContain == _MATK)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_magicalAttackBonus += strList[0].toInt();
        }
        else if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_magicalAttack += strList[1].toInt();
        }
    }
    else if(strContain == _PCRIP)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_physicalCriticalProb += strList[0].toInt();
        }
    }
    else if(strContain == _MCRIP)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_magicalCriticalProb += strList[0].toInt();
        }
    }
    else if(strContain == _PCRIV)
    {
        if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_physicalCriticalValue += strList[1].toInt();
        }
    }
    else if(strContain == _MCRIV)
    {
        if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_magicalCriticalValue += strList[1].toInt();
        }
    }
    else if(strContain == _SKI)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_skillBonus += strList[0].toInt();
        }
    }
    else if(strContain == _DAM)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_damageBonus += strList[0].toInt();
        }
    }
    else if(strContain == _ELE)
    {
        if(str.contains("+"))
        {
            QStringList strList = str.split("+");
            ad_elementBonus += strList[1].toInt();
        }
    }
    else if(strContain == _CRIDAM)
    {
        if(str.contains("%") && str.contains("+"))
        {
            QStringList strList = str.split("+")[1].split("%");
            ad_criticalDamageBonus += strList[0].toInt();
        }
    }
    else if(strContain == _EXT)
    {
        if(str.contains("%") && str.contains(tr("，")))
        {
            QStringList strList;
            if(str.contains(tr("，")))
            {
                strList = str.split("，")[1].split("%");
            }
            else
            {
                strList = str.split("%");
            }
            QString temp;
            temp = strList[0];
            temp = temp.right(temp.size() - 1 - temp.lastIndexOf(tr("加")));
            ad_extraDamageBonus += temp.toInt();
        }

    }
    else if(strContain == _ELEEXT)
    {
        if(str.contains("%") && str.contains(tr("，")))
        {
            QStringList strList;
            if(str.contains(tr("，")))
            {
                strList = str.split("，")[1].split("%");
            }
            else
            {
                strList = str.split("%");
            }
            QString temp;
            temp = strList[0];
            temp = temp.right(temp.size() - 1 - temp.lastIndexOf(tr("加")));
            ad_elementExtraDamageBonus += temp.toInt();
        }
    }
    if(ui->comboBox_eqType->currentText() == tr("武器"))
        entryValueIntoSpinbox(0);
    else if(ui->comboBox_eqType->currentText() == tr("防具"))
        entryValueIntoSpinbox(1);
    else if(ui->comboBox_eqType->currentText() == tr("首饰"))
        entryValueIntoSpinbox(2);
    else if(ui->comboBox_eqType->currentText() == tr("辅助装备"))
        entryValueIntoSpinbox(3);
}

void AddEquip::checkBoxOnlyOne()
{
    QCheckBox *checkbox = qobject_cast<QCheckBox*>(sender());
    if(ui->comboBox_eqType->currentText() == tr("防具"))
    {
        if(ui->checkBox_clothArmor!=checkbox && ui->checkBox_clothArmor->isChecked())
        {
            ui->checkBox_clothArmor->toggle();
        }
        if(ui->checkBox_leatherArmor!=checkbox && ui->checkBox_leatherArmor->isChecked())
        {
            ui->checkBox_leatherArmor->toggle();
        }
        if(ui->checkBox_lightArmor!=checkbox && ui->checkBox_lightArmor->isChecked())
        {
            ui->checkBox_lightArmor->toggle();
        }
        if(ui->checkBox_heavyArmor!=checkbox && ui->checkBox_heavyArmor->isChecked())
        {
            ui->checkBox_heavyArmor->toggle();
        }
        if(ui->checkBox_plateArmor!=checkbox && ui->checkBox_plateArmor->isChecked())
        {
            ui->checkBox_plateArmor->toggle();
        }
    }
    else if(ui->comboBox_eqType->currentText() == tr("武器"))
    {
        if(ui->checkBox_knight!=checkbox && ui->checkBox_knight->isChecked())
        {
            ui->checkBox_knight->toggle();
        }
        if(ui->checkBox_shooter!=checkbox && ui->checkBox_shooter->isChecked())
        {
            ui->checkBox_shooter->toggle();
        }
        if(ui->checkBox_fighter!=checkbox && ui->checkBox_fighter->isChecked())
        {
            ui->checkBox_fighter->toggle();
        }
        if(ui->checkBox_clergy!=checkbox && ui->checkBox_clergy->isChecked())
        {
            ui->checkBox_clergy->toggle();
        }
        if(ui->checkBox_enchanter!=checkbox && ui->checkBox_enchanter->isChecked())
        {
            ui->checkBox_enchanter->toggle();
        }
    }
}
